package org.halk.request;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import org.junit.Before;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.net.URL;
import java.util.List;

/**
 * 解析json字符串
 * @Author halk
 * @Date 2020/9/10 16:54
 */
public class JsonPare {

    JSON json1, json2;

    @Before
    public void test11() throws FileNotFoundException {
        URL url = this.getClass().getClassLoader().getResource("testFile/test1.json");
        List<String> strings = FileUtil.readLines(url, "utf-8");
        String str = String.join("", strings);
        json1 = JSONUtil.parse(str);

        URL url2 = this.getClass().getClassLoader().getResource("testFile/test2.json");
        List<String> strings2 = FileUtil.readLines(url2, "utf-8");
        String str2 = String.join("", strings2);
        json2 = JSONUtil.parse(str2);
    }

    @Test
    public void test01() {
        List<Integer> coutList = (List<Integer>) JSONUtil.getByPath(json1, "content.currentTaskCompleteCount");
        coutList.forEach(x -> System.out.println(x));
        System.out.println(coutList.stream().reduce(Integer::sum).get());
//        System.out.println(coutList.stream().reduce(Integer::sum).get());

//        List<Integer> coutGeneralizedList = (List<Integer>) JSONUtil.getByPath(json1, "content.completedCountGeneralized");
//        coutGeneralizedList.forEach(x -> System.out.print(x  + " , "));
//        System.out.println();
//        System.out.println(coutGeneralizedList.stream().reduce(Integer::sum).get());
    }

    @Test
    public void test21() {
        List<String> list = (List<String>) json2.getByPath("content.subjectId");
        list.forEach(x -> System.out.print(x + ","));
        System.out.println();
        System.out.println(list.size());
    }


}
